package com.bos.editor;


import com.bos.dao.PengelolaDAO;
import com.bos.domain.Pengelola;

import java.beans.PropertyEditorSupport;

/**
 * User: Deny Prasetyo, S.T.
 * http://www.jasoet.com
 */
public class PengelolaEditor extends PropertyEditorSupport {

    private PengelolaDAO pengelolaDAO;

    public PengelolaEditor(PengelolaDAO pengelolaDAO) {
        this.pengelolaDAO = pengelolaDAO;
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        try {
            long id = Long.parseLong(text);

            Pengelola result = pengelolaDAO.get(id);

            if (result != null) {
                setValue(result);
            } else {
                throw new IllegalArgumentException("Pengelola with id " + id + " not Found!");
            }

        } catch (NumberFormatException ex) {
            throw new IllegalArgumentException(ex.getMessage());
        }
    }

    @Override
    public String getAsText() {
        if (getValue() != null) {
            Pengelola result = (Pengelola) getValue();
            return result.getId() + "";
        } else {
            return "";
        }
    }
}
